//
//  HomeHeadView.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "HomeHeadView.h"
#import "ZYBannerView.h"

@interface HomeHeadView ()<ZYBannerViewDataSource, ZYBannerViewDelegate>

@property (nonatomic, strong) ZYBannerView *banner;
@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;
@property (nonatomic, strong)UIButton * fourButton;
@property (nonatomic, strong)UILabel * oneLabel;
@property (nonatomic, strong)UILabel * twoLabel;
@property (nonatomic, strong)UILabel * threeLabel;
@property (nonatomic, strong)UILabel * fourLabel;
@property (nonatomic, strong)UIButton * fiveButton;
@property (nonatomic, strong)UIButton * sixButton;
@property (nonatomic, strong)UIButton * sevenButton;
@property (nonatomic, strong)UIButton * eightButton;
@property (nonatomic, strong)UILabel * fiveLabel;
@property (nonatomic, strong)UILabel * sixLabel;
@property (nonatomic, strong)UILabel * sevenLabel;
@property (nonatomic, strong)UILabel * eightLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIView * verLineView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, strong)UIView * bottomLineView;
@property (nonatomic, strong)UIView * verOneLineView;
@property (nonatomic, strong)UIView * verTwoLineView;
@property (nonatomic, strong)UIView * verThreeLineView;

@property (nonatomic, strong)NSArray * dataArray;

@end

@implementation HomeHeadView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.dataArray = @[@"400101654_banner",@"erw565656",@"opo5"];
    
    self.backgroundColor = [UIColor whiteColor];
    // 配置banner
    [self setupBanner];
    
    self.oneButton = [self setupButtonWith:@"fffg"];
    [self addSubview:self.oneButton];
    
    self.twoButton = [self setupButtonWith:@"ff"];
    [self addSubview:self.twoButton];
    
    self.threeButton = [self setupButtonWith:@"33ff"];
    [self addSubview:self.threeButton];
    
    self.fourButton = [self setupButtonWith:@"sdfdsf656"];
    [self addSubview:self.fourButton];
    
    self.oneLabel = [self setupLabelWith:@"我要爆料"];
    [self addSubview:self.oneLabel];
    
    self.twoLabel = [self setupLabelWith:@"房屋租赁"];
    [self addSubview:self.twoLabel];
    
    self.threeLabel = [self setupLabelWith:@"人才招聘"];
    [self addSubview:self.threeLabel];
    
    self.fourLabel = [self setupLabelWith:@"热点"];
    [self addSubview:self.fourLabel];
    
    
    self.fiveButton = [self setupButtonWith:@"dfds2f123"];
    [self addSubview:self.fiveButton];
    
    self.sixButton = [self setupButtonWith:@"wedwq45"];
    [self addSubview:self.sixButton];
    
    self.sevenButton = [self setupButtonWith:@"wqewq21022"];
    [self addSubview:self.sevenButton];
    
    self.eightButton = [self setupButtonWith:@"vfd15sds"];
    [self addSubview:self.eightButton];
    
    self.fiveLabel = [self setupLabelWith:@"天气预报"];
    [self addSubview:self.fiveLabel];
    
    self.sixLabel = [self setupLabelWith:@"婚姻匹配"];
    [self addSubview:self.sixLabel];
    
    self.sevenLabel = [self setupLabelWith:@"健康知识"];
    [self addSubview:self.sevenLabel];
    
    self.eightLabel = [self setupLabelWith:@"历史上的今天"];
    [self addSubview:self.eightLabel];
    
    self.bottomLineView = [self setupLineView];
    [self addSubview:self.bottomLineView];
    
    self.verOneLineView = [self setupLineView];
    [self addSubview:self.verOneLineView];
    
    self.verTwoLineView = [self setupLineView];
    [self addSubview:self.verTwoLineView];
    
    self.verThreeLineView = [self setupLineView];
    [self addSubview:self.verThreeLineView];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self addSubview:self.lineView];
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.bottomView = bottomView;
    
    [self addSubview:self.bottomView];
    
    UIView * verLineView = [UIView new];
    
    verLineView.backgroundColor = MAINCOLOR;
    
    self.verLineView = verLineView;
    
    [self addSubview:self.verLineView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = MAINCOLOR;
    titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    titleLabel.text = @"头条推荐";
    
    self.titleLabel = titleLabel;
    
    [self addSubview:self.titleLabel];
}


- (void)layoutSubviews{
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(25 * SCREENH6);
        make.height.width.mas_equalTo(60 * SCREENH6);
        make.top.mas_equalTo(self.banner.mas_bottom).offset(17.5 * SCREENH6);
    }];
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.oneButton);
        make.top.mas_equalTo(self.oneButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.oneButton);
        make.leading.mas_equalTo(self.oneButton.mas_trailing).offset(30 * SCREENH6);
    }];
    
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.twoButton);
        make.top.mas_equalTo(self.twoButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.twoButton);
        make.leading.mas_equalTo(self.twoButton.mas_trailing).offset(25 * SCREENH6);
    }];
    
    [self.threeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.threeButton);
        make.top.mas_equalTo(self.threeButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.fourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.threeButton);
        make.leading.mas_equalTo(self.threeButton.mas_trailing).offset(30 * SCREENH6);
    }];
    
    [self.fourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fourButton);
        make.top.mas_equalTo(self.fourButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.oneLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.fiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.height.width.mas_equalTo(self.oneButton);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(2 * SCREENH6);
    }];
    
    [self.fiveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fiveButton);
        make.top.mas_equalTo(self.fiveButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.sixButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.fiveButton);
        make.leading.mas_equalTo(self.fiveButton.mas_trailing).offset(30 * SCREENH6);
    }];
    
    [self.sixLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.sixButton);
        make.top.mas_equalTo(self.sixButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.sevenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.sixButton);
        make.leading.mas_equalTo(self.sixButton.mas_trailing).offset(25 * SCREENH6);
    }];
    
    [self.sevenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.sevenButton);
        make.top.mas_equalTo(self.sevenButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.eightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.sevenButton);
        make.leading.mas_equalTo(self.sevenButton.mas_trailing).offset(30 * SCREENH6);
    }];
    
    [self.eightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.eightButton);
        make.top.mas_equalTo(self.eightButton.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(3 * SCREENH6);
        make.bottom.mas_equalTo(self.lineView.mas_bottom).offset(92 * SCREENH6);
    }];
    
    [self.verTwoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1 * SCREENH6);
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.banner.mas_bottom);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
    [self.verOneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.mas_equalTo(self.verTwoLineView);
        make.trailing.mas_equalTo(self.verTwoLineView.mas_leading).offset(-87 * SCREENH6);
    }];
    
    [self.verThreeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.mas_equalTo(self.verTwoLineView);
        make.leading.mas_equalTo(self.verTwoLineView.mas_trailing).offset(87 * SCREENH6);
    }];
    
    [self.verLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bottomView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(3 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.height.mas_equalTo(23 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.verLineView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.verLineView);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
        make.bottom.mas_equalTo(self);
    }];
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    return lineView;
}

- (UILabel *)setupLabelWith:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ColorWithRGB(0x93ACB5);
    label.font = [UIFont systemFontOfSize:12 * SCREENH6];
    label.text = string;
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}

- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setImage:ImageNamed(string) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)ClickButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.oneButton) {
        
        type = @"one";
    }else if (button == self.twoButton){
        
        type = @"two";
    }else if (button == self.threeButton){
        
        type = @"three";
    }else if (button == self.fourButton){
        
        type = @"four";
    }else if (button == self.fiveButton){
        
        type = @"five";
    }else if (button == self.sixButton){
        
        type = @"six";
    }else if (button == self.sevenButton){
        
        type = @"seven";
    }else if (button == self.eightButton){
        
        type = @"eight";
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickHeaderViewButton" object:nil userInfo:dict];    
}

- (void)setupBanner
{
    // 初始化
    self.banner = [[ZYBannerView alloc] init];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
//    self.banner.pageControlFrame = CGRectMake(200, 195, 45, 32);
    
    [self addSubview:self.banner];
    
    // 设置frame
    self.banner.frame = CGRectMake(0,0,SCREEN_WIDTH,205 * SCREENH6);
}

#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
    //    NSString * imageName = self.dataArray[index];
    //    NSURL * url = [NSURL URLWithString:imageName];
    //    //    NSLog(@"url==%@",url);
    //    // 创建将要显示控件
    //    UIImageView *imageView = [[UIImageView alloc] init];
    //    //    imageView.image = [UIImage imageNamed:imageName];
    //    [imageView sd_setImageWithURL:url];
    //    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"轮播1"]];
    //    imageView.contentMode = UIViewContentModeScaleAspectFill;
    //
    //    return imageView;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    imageView.image = [UIImage imageNamed:self.dataArray[index]];
    
    return imageView;
}

#pragma mark - ZYBannerViewDelegate
// 在这里实现点击事件的处理
- (void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index
{
    //    NSLog(@"点击了第%ld个项目", (long)index);
}

- (void)banner:(ZYBannerView *)banner didScrollToItemAtIndex:(NSInteger)index
{
    //    NSLog(@"滚动到第%ld个项目", (long)index);
}


@end
